<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_coa extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_coa_model', 'Coa');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Akun";
		viewByTemplate('keu_coa/index', $data, 'grid');
	}
	function getdata()
	{
		echo $this->Coa->GetJSON();
	}
	
	
	function combotree_data($as_office_code=''){
		$this->secure_session();
		echo json_encode($this->Coa->GetComboTreeData($as_office_code));
	}
	
	function combogrid_data(){
		$this->secure_session();
		echo json_encode($this->Coa->GetComboGridData());
	}
	
	function create()
	{
		$data['title'] = "Tambah Akun";
		$data['data'] = NULL;
		$data['id_coa_induk'] = get('id_coa');
		
		
		$data['mode'] = "create";
		viewByTemplate('keu_coa/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Ubah Akun";
		$data['data'] = $this->Coa->GetSingleView(get('id_coa'));
		$data['id_coa_induk'] = $data['data']['id_coa_induk'];

		$data['mode'] = "edit";
		viewByTemplate('keu_coa/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		if (post("id_coa") == '') {
			$error->Add("id_coa", "Akun", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM KEU_COA WHERE ID_COA = '".@strtoupper(@trim(post("id_coa"))))."'") > 0) {
			$error->Add("id_coa", "Akun", $tpl_duplicate);
		}

		if (post("nama_coa") == '') {
			$error->Add("nama_coa", "Nama Akun", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('id_coa', 'nama_coa', 'id_coa_induk', 'status_pos', 'status_tipe'));
				
				if (post("mode") == "create")
				{
					$data['CREATE_DATE']	= NOW;
					$data['CREATE_USER']	= USERID;

					$this->Coa->Add($data);

					$tpl_insert_success = $this->MessageTemplate("CS001");
					$ls_response = $tpl_insert_success['message_id'];
		

				}
				else if (post("mode") == "edit")
				{
					$data['MODIFY_DATE']	= NOW;
					$data['MODIFY_USER']	= USERID;
					
					$this->Coa->Update($data, 
										array('ID_COA'	=> $data['id_coa']));

					$tpl_update_success = $this->MessageTemplate("CS002");
					$ls_response = $tpl_update_success['message_id'];

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_coa	= get('id_coa');
		
		if ($id_coa != "")
		{
			// Delete Single
			$ls_result = $this->Coa->DeleteSingle($id_coa);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->Coa->DeleteSingle($row['id_coa']);
			}
		}
		
		echo $ls_result;
	}
	
	function print_coa(){
			$this->load->helper('fpdf');
			$data['data']	   = $this->Coa->GetReportData();
			
			$this->load->view('keu_coa/print_coa', $data);	
	}
}
?>